//#include <base.js>
//#include <openlayersExt.js>
//#include <event.js>


/**
* 
* Factory per creazione di layers di tipo OL
* 
* @namespace CWN2
* @class layerFactory
* 
*/

CWN2.layerFactory = {

	/**
	* 
	* Create an OpenLayers layer based on a given configuration
	* 
	* @method createLayer
	* 
	* @param {layerConfig} layerConfig Obj containing the layer configuration 
	* 
	* @return: 	{OpenLayers.Layer}
	* 
	*/ 
	
	createLayer: function(layerConfig) {

		return CWN2.layerFactory[layerConfig.type](layerConfig);
	
	},
	
	/*
	* 
    * Metodi per creazione dei layer Openlayers dei vari tipi
    *
    */ 
    
    // google stradario
	google_roadmap: function(layerConfig) {
		if (CWN2.base.DEBUG) {
			console.log('CWN2.layerFactory.createLayer google_roadmap');
		}
		var layerType = google.maps.MapTypeId.ROADMAP;
		return CWN2.layerFactory.google(layerType,layerConfig);
	},
	
    // google satellite
	google_satellite: function(layerConfig) {
		if (CWN2.base.DEBUG) {
			console.log('CWN2.layerFactory.createLayer google_satellite');
		}
		var layerType = google.maps.MapTypeId.SATELLITE;
		return CWN2.layerFactory.google(layerType,layerConfig);
	},
	
	// google generico
	google: function(layerType,layerConfig) {
		var	olLayer = new OpenLayers.Layer.Google(
			layerConfig.name,
			{
				type: layerType,
				minZoomLevel: layerConfig.minZoomLevel, 
				maxZoomLevel: layerConfig.maxZoomLevel 
			}
		);
		return olLayer;
	},
	
	// Vettoriale GeoJSON
	GeoJSON: function(layerConfig) {
		if (CWN2.base.DEBUG) {
			console.log('CWN2.layerFactory.createLayer GeoJSON');
		}
		
		// impostazione opzioni
		var layerOptions = {};
		layerOptions.projection = layerConfig.projection || 'EPSG:900913';
		if (layerConfig.url !== undefined){
			layerOptions.protocol = new OpenLayers.Protocol.HTTP({
					url: layerConfig.url,
					format: new OpenLayers.Format.GeoJSON({})
				});
			layerOptions.strategies = layerConfig.strategies || [new OpenLayers.Strategy.Fixed()];
	
		}
		layerOptions.opacity = layerConfig.opacity || 1;
		layerOptions.attribution = layerConfig.attribution;
		//layerOptions.transistionEffect = 'resize';


		// creo il livello OL
		var olLayer = new OpenLayers.Layer.Vector(
			layerConfig.name,
			layerOptions
		);

		// gestione stile
		if (layerConfig.styleMap !== undefined){
			olLayer.styleMap = new OpenLayers.StyleMap(layerConfig.styleMap);			
		}
		return olLayer;
	},
	
	// WMS
	WMS: function(layerConfig) {
		if (CWN2.base.DEBUG) {
			console.log('CWN2.layerFactory.createLayer WMS');
		}

		// impostazione parametri layer WMS
		var wmsParams = {};
		wmsParams.layers = layerConfig.wmsParams.name;
		wmsParams.transparent = layerConfig.wmsParams.transparent;		

		// impostazione opzioni
		var layerOptions = {};
		layerOptions.projection = layerConfig.projection || 'EPSG:900913';
		layerOptions.isBaseLayer = layerConfig.isBaseLayer;
		layerOptions.opacity = layerConfig.opacity || 1;
		layerOptions.attribution = layerConfig.attribution;
		layerOptions.transistionEffect = 'resize';
		layerOptions.singleTile = true;
		layerOptions.metadata = layerConfig.metadata;
		
		// creo il livello OL
		var olLayer = new OpenLayers.Layer.WMS(
			layerConfig.name,
			layerConfig.wmsParams.url,
			wmsParams,
			layerOptions
		);

		return olLayer;
	}
	

};
